home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / tutorial / trac.zip / LESSON3 < prev    next >
Text File  |  1990-02-09  |  25KB  |  542 lines

  1.  
  2.  
  3.  
  4.                          ELEMENTARY COMPUTER PROGRAMMING
  5.                                     Lesson 3
  6.         
  7.                                  Copyright 1990
  8.                           Castle Oaks Computer Services
  9.                               Post Office Box 36082
  10.                            Indianapolis, IN 46236-0082
  11.         
  12.         In the first two sessions some of the basics of computers were 
  13.         covered.  In addition, the specifics of TRAC, a hypothetical 
  14.         computer, were also covered.  Computer users very seldom program 
  15.         a computer in its own language.  Occasionally, a user will create 
  16.         a short program or patch a larger program by using a utility that 
  17.         will facilitate doing so.
  18.         
  19.         Machine language can be very tedious as we have seen using TRAC 
  20.         which is fairly easy to use.  Furthermore, it is difficult to 
  21.         make changes in machine language since coding is usually sequen-
  22.         tial; thus, any addition means modifying most subsequent instruc-
  23.         tions.  The easiest way to add instructions to an existing ma-
  24.         chine language program is to:
  25.         
  26.         replace one existing instruction with an unconditional branch to 
  27.         someplace in unused memory, 
  28.         
  29.         at the new location put in the instruction you removed,
  30.         
  31.         add the new instructions,
  32.         
  33.         followed by an unconditional branch back to the next consecutive 
  34.         location after the the instruction you previously replaced.
  35.         
  36.         Suppose, for example, we have a program that reads two numbers, 
  37.         adds them and then displays the result.  Figure III-1 illustrates 
  38.         the original code.
  39.         
  40.         -----------------------------------------------------------------
  41.         0010     401001 Read in A and B (also C, D, and E)
  42.         0011     001001 Load A
  43.         0012     011002 Add B
  44.         0013     031003 Store answer at C
  45.         0014     411001 Display result
  46.         0015     500000 Halt
  47.         9999     000010 End of code
  48.         
  49.         Figure III-1
  50.         -----------------------------------------------------------------
  51.         
  52.         Now suppose that we wish to modify this program so that it adds 
  53.         four numbers instead of two.  For such a short program, it is 
  54.         easy to rewrite the whole program.  But to illustrate how a large 
  55.         program might be patched, consider Figure III-2.  
  56.         
  57.         
  58.         
  59.         
  60.         
  61.                                       III-1
  62.  
  63.  
  64.         In the modified program, we have replaced the add instruction at 
  65.         location 0012 with an unconditional branch to 1501.  Then the 
  66.         instruction formerly at 0012 is re-entered at 1501, the new in-
  67.         structions follow with a branch back to location 0013.  In this 
  68.         case we also have to change the instruction at 0013.  Note that 
  69.         the new code must be inserted before the end-of-code line.
  70.         
  71.         -----------------------------------------------------------------
  72.         0010     401000 Read in A and B (also C, D, and E)
  73.         0011     001001 Load A
  74.         0012     261501 Branch unconditionally to 1501
  75.         0013     031005 Store answer at E
  76.         0014     411001 Display result
  77.         0015     500000 Halt
  78.         1501     011002 Add B
  79.         1502     011003 Add C
  80.         1503     011004 Add D
  81.         1504     260013 Branch back
  82.         9999     000010 End of code
  83.         
  84.         Figure III-2
  85.         -----------------------------------------------------------------
  86.         
  87.         Besides being hard to modify, machine code is designed for ma-
  88.         chines, not people.  In order to make programming more user-
  89.         friendly, higher order languages have been developed.  A program-
  90.         mer writes his code in one of these languages and then he uses a 
  91.         program to translate this code into machine language.
  92.         
  93.         There are two categories of higher order languages.  They are:
  94.         
  95.         COMPILERS - These programs take simple statements such as 
  96.         A = B + C and translates them into several machine language 
  97.         instructions.  This is called a one-to-many translation.  Exam-
  98.         ples of higher order languages are BASIC, C, COBOL, FORTRAN, 
  99.         PASCAL, etc.  These languages are almost totally computer inde-
  100.         pendent.  That is, a program written in one of these languages 
  101.         can be translated to the machine language of different kinds of 
  102.         computers.
  103.         
  104.         ASSEMBLERS - These programs provide only a one-to-one transla-
  105.         tion.  These programs are machine dependent and there must be a 
  106.         different assembler for each different type of computer.  An 
  107.         example assembly language program fragment is:
  108.         
  109.                                     LD B
  110.                                     AD C
  111.                                     ST A
  112.         
  113.         A program written in a higher order language is called the source 
  114.         code and the resultant machine code, after translation, is called 
  115.         object code.  Usually the translation is performed on the machine 
  116.         the code is going to run on; however, in some environments, the 
  117.         machine code is generated on a different computer.  The program 
  118.         used is called a cross-compiler or cross-assembler.  For example,
  119.         
  120.         
  121.                                       III-2
  122.  
  123.  
  124.         a program for a computer that is to be integrated into a machine 
  125.         (e.g. automobile, toaster, VCR, etc.) would be developed on a 
  126.         large computer before the code is inserted in the target comput-
  127.         er.
  128.         
  129.         In this course, we will be discussing the assembly program for 
  130.         TRAC.  It is called TRAP (TRaining computer Assembly Program).  
  131.         The primary purpose of this course is to introduce you to an 
  132.         assembly program.  TRAP will provide a simple language for that 
  133.         purpose.  After mastering TRAP, you should find it easier to 
  134.         learn other assemblers.  When a training facility offers a course 
  135.         in "machine language" for a particular computer, it is usually a 
  136.         course in the assembly language used for that computer.
  137.         
  138.         TRAP Coding Format
  139.         
  140.         Figure III-3 shows the format required for TRAP.
  141.         
  142.         -----------------------------------------------------------------
  143.         
  144.         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------------------------+
  145.         |0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1                             8|
  146.         |1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7                             0|
  147.         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------------------------+
  148.         |Loc-   | |     |T|Op |Address| |Comments                       |
  149.         |  ation| |     |a|co-|       | |                               |
  150.         |       | |     |g| de|       | |                               |
  151.         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------------------------+
  152.         
  153.         Figure III-3
  154.         -----------------------------------------------------------------
  155.         
  156.         This format must be adhered to strictly except where noted other-
  157.         wise.  Note that the format for TRAP coding is very similar to 
  158.         that of TRAC.  In fact, you may enter machine code in the TRAP 
  159.         format for use directly on TRAC.  An explanation of the format is 
  160.         as follows:
  161.         
  162.         The first four columns are reserved for a memory location.  This 
  163.         location may be entirely numeric, it may be mnemonic (i.e. it may 
  164.         be a symbolic location such as "XVEL"), or it may be blank.  The 
  165.         very first line of code MUST have a numeric machine address.  It 
  166.         specifies the machine origin of the code.  A mnemonic address may 
  167.         be any combination of letters and numbers (at least one must be 
  168.         non-numeric).  The mnemonic is case sensitive (although upper 
  169.         case only is recommended); and it is position sensitive.  That 
  170.         is, the following mnemonic locations are all different: (a   ), 
  171.         ( a  ), (  a ), (   a), (A   ), ( A  ), (  A ), and (   A).  More 
  172.         will be said about mnemonic locations later.  
  173.         
  174.         Column 5 must always be blank.
  175.         
  176.         Columns 6 through 15 may be used for constants.  A constant may 
  177.         be positioned anyplace within the field but it must not exceed 
  178.         nine digits plus sign.
  179.         
  180.         
  181.                                       III-3
  182.  
  183.  
  184.         Columns 6 through 8 must be blank on instructions.  
  185.         
  186.         Column 9 is the tag field and is used just as in TRAC. That is, 
  187.         it may contain only a single digit, 1 through 9, or blank.
  188.         
  189.         Columns 10 and 11 are for the operation code.  This field may 
  190.         contain either an actual numeric code or any one of the mnemonic 
  191.         codes shown in Lesson 1.  A mnemonic code may be either upper or 
  192.         lower case but not both upper and lower case.  Assembly programs 
  193.         usually contain some pseudo operation codes which are used as 
  194.         instructions to the assembler.  TRAP has only one of these and it 
  195.         is EN.  This signifies the ENd of code.  There may be only one of 
  196.         these and it must be in the very last line of code.  The EN 
  197.         instruction must contain, in the address field, the location 
  198.         where execution of the program is to begin.  It will generate the 
  199.         9999 line required by TRAC.
  200.         
  201.         Columns 12 through 15 are the address field.  This field may 
  202.         contain an actual machine location, a mnemonic location, or a 
  203.         relative address.  Any mnemonic address used in the address field 
  204.         must appear once and only once in the location field of some 
  205.         instruction or constant.  A relative address is one that is 
  206.         defined relative to the location of the current instruction.  A 
  207.         relative address must have an asterisk, "*", in column 12.  It 
  208.         may be followed by a plus or minus sign and a one or two digit 
  209.         number.  Thus, *+1 refers to the next location, * refers to the 
  210.         current location, and *-1 refers to the previous location.  
  211.         Although relative addresses of *+99 and *-99 are possible, it is 
  212.         recommended that relative addressing be confined to the near 
  213.         neighborhood of the current location.
  214.         
  215.         Column 16 must be blank.
  216.         
  217.         Columns 17 through 80 may be used for comments.  They are carried 
  218.         over to the object code but are ignored otherwise.  It is recom-
  219.         mended that you not use beyond column 64 to avoid lines over 80 
  220.         columns in the object code. 
  221.         
  222.         Figure III-4 gives a TRAP program of the problem coded earlier in 
  223.         Figure II-1.
  224.         
  225.         
  226.         
  227.         
  228.         
  229.         
  230.         
  231.         
  232.         
  233.         
  234.         
  235.         
  236.         
  237.         
  238.         
  239.         
  240.         
  241.                                       III-4
  242.  
  243.  
  244.         -----------------------------------------------------------------
  245.         0010     RD   X READ X,Y
  246.                  LD   X LOAD X
  247.                  AD   Y ADD Y
  248.                  ST ANS STORE SUM
  249.                  PC   X PRINT ON CONSOLE
  250.                  LD   X LOAD X
  251.                  SU   Y SUBTRACT Y
  252.                  ST ANS STORE DIFFERENCE
  253.                  PC   X PRINT ON CONSOLE
  254.                  HT*
  255.            X          0
  256.            Y          0
  257.          ANS          0
  258.                  EN0010
  259.         
  260.         Figure III-4
  261.         -----------------------------------------------------------------
  262.         
  263.         Note that in Figure III-4, three lines of coding had to be added 
  264.         to define locations for X, Y, ANS.  This is to comply with the 
  265.         rule, "Every mnemonic location given in the address field must be 
  266.         defined once and only once in the location field."  This coding 
  267.         will assign X, Y, and ANS to locations 0020, 0021, and 0022, 
  268.         respectively.
  269.         
  270.         TRAP does not relieve us of all the responsibility about machine 
  271.         locations.  Note that in the example of Figure III-4 that the 
  272.         read instruction will cause values to be read into five locations 
  273.         starting at location X.  Therefore, we must be sure that none of 
  274.         these are used for instructions or constants.
  275.         
  276.         As an example of how we might force the assembler to make the 
  277.         same assignments as we made in Figure II-1, see Figure III-5.
  278.         
  279.         -----------------------------------------------------------------
  280.         0010     RD   X READ X,Y
  281.                  LD   X LOAD X
  282.                  AD   Y ADD Y
  283.                  ST ANS STORE SUM
  284.                  PC   X PRINT ON CONSOLE
  285.                  LD   X LOAD X
  286.                  SU   Y SUBTRACT Y
  287.                  ST ANS STORE DIFFERENCE
  288.                  PC   X PRINT ON CONSOLE
  289.                  HT*
  290.         0099          0 Dummy line to re-origin
  291.            X          0
  292.            Y          0
  293.          ANS          0
  294.                  EN0010
  295.         
  296.         Figure III-5
  297.         -----------------------------------------------------------------
  298.         
  299.         
  300.         
  301.                                       III-5
  302.  
  303.  
  304.         The code of Figure III-5 will force X, Y, and ANS to use the 
  305.         locations 0100, 0101, and 0102, respectively.  Furthermore, if we 
  306.         re-origin, the order does not have to be as shown in the above 
  307.         figure.  We might choose to do it as shown in Figure III-6.
  308.         
  309.         -----------------------------------------------------------------
  310.         0099          0 Dummy line to origin program
  311.            X          0
  312.            Y          0
  313.          ANS          0
  314.         0009          0 Dummy line to re-origin
  315.         STRT     RD   X READ X,Y
  316.                  LD   X LOAD X
  317.                  AD   Y ADD Y
  318.                  ST ANS STORE SUM
  319.                  PC   X PRINT ON CONSOLE
  320.                  LD   X LOAD X
  321.                  SU   Y SUBTRACT Y
  322.                  ST ANS STORE DIFFERENCE
  323.                  PC   X PRINT ON CONSOLE
  324.                  HT*
  325.                  ENSTRT
  326.         
  327.         Figure III-6
  328.         -----------------------------------------------------------------
  329.         
  330.         Note now that we have given a mnemonic name to the location where 
  331.         the execution of the program is to begin.
  332.         
  333.         Next we will show another program to illustrate the use of both 
  334.         mnemonic locations and relative addressing.  Consider the flow 
  335.         chart of Figure III-7.
  336.         
  337.         -----------------------------------------------------------------
  338.                               |
  339.                               v
  340.                         --------------               -----------------
  341.                        / Read OP,X,Y /<-------------/ Print OP,X,Y,Z /
  342.                        --------------               -----------------
  343.                               |                          ^      ^
  344.                               v            .             |      |
  345.                              / \          / \            |      |
  346.                +-------+  = /   \ >      /   \  =    +-------+  |
  347.                | Z=X+Y |<--<OP:1 >-----><OP:2 >----->| Z=X-Y |  |
  348.                +-------+    \   /        \   /       +-------+  |
  349.                    |         \./          \./                   |
  350.                    |          | <          | not =              |
  351.                    |          |            |                    |
  352.                    |          |  +------+  |                    |
  353.                    |          +->| STOP |<-+                    |
  354.                    |             +------+                       |
  355.                    |                                            |
  356.                    +--------------------------------------------+
  357.         
  358.         Figure III-7
  359.         -----------------------------------------------------------------
  360.         
  361.                                       III-6
  362.  
  363.  
  364.         In this program we read in three numbers.  If the first number is 
  365.         a "1", we add the other two numbers; if it is a "2", we subtract 
  366.         the third number from the second; any other code in the first 
  367.         number stops the program.  Figure III-8 gives a TRAP coding.
  368.         
  369.         -----------------------------------------------------------------
  370.         0999          0 Origin
  371.           OP          0
  372.            X          0
  373.            Y          0
  374.            Z          0
  375.                       0 Dummy included to reserve read space
  376.          ONE          1 Constant one
  377.         STRT     RD  OP Read OP, X, Y
  378.                  LD  OP Load OP
  379.                  SU ONE Find difference for test
  380.                  BNSTOP If < 1 go to halt
  381.                  BZ ADD If = 1 go to add routine
  382.                  SU ONE If > 1 see if it is 2
  383.                  BZ SUB If = 2 go to subtract
  384.         STOP     HT0000 If not stop
  385.          SUB     LD   X Load X
  386.                  SU   Y Subtract Y
  387.                  ST   Z Store at Z
  388.                  BUPRNT Go to print
  389.          ADD     LD   X Load X
  390.                  AD   Y Add Y
  391.                  ST   Z Store at Z
  392.         PRNT     PC  OP Print
  393.                  BUSTRT Go back to start
  394.                  ENSTRT End of code with starting location
  395.         
  396.         Figure III-8
  397.         -----------------------------------------------------------------
  398.         
  399.         Note that in this example, the program was origined at 0999.  
  400.         This was done to force the read area to start at 1000.  Also, a 
  401.         dummy line was added after the declaration of Z.  This was to 
  402.         reserve space for the read.  If this line were omitted, the 
  403.         constant, ONE, would be replaced by zero on the first execution 
  404.         of a read.  The assembler was allowed to assign locations for the 
  405.         executable code consecutively with the area for variables and 
  406.         constants.
  407.         
  408.         When assembled, the code in Figure III-8a results.
  409.         
  410.         
  411.         
  412.         
  413.         
  414.         
  415.         
  416.         
  417.         
  418.         
  419.         
  420.         
  421.                                       III-7
  422.  
  423.  
  424.         -----------------------------------------------------------------
  425.         0999          0 0999          0 Origin
  426.         1000          0   OP          0
  427.         1001          0    X          0
  428.         1002          0    Y          0
  429.         1003          0    Z          0
  430.         1004          0               0 Dummy included to reserve read space
  431.         1005          1  ONE          1 Constant one
  432.         1006     401000 STRT     RD  OP Read OP, X, Y
  433.         1007     001000          LD  OP Load OP
  434.         1008     021005          SU ONE Find difference for test
  435.         1009     241013          BNSTOP If < 1 go to halt
  436.         1010     251018          BZ ADD If = 1 go to add routine
  437.         1011     021005          SU ONE If > 1 see if it is 2
  438.         1012     251014          BZ SUB If = 2 go to subtract
  439.         1013     500000 STOP     HT0000 If not stop
  440.         1014     001001  SUB     LD   X Load X
  441.         1015     021002          SU   Y Subtract Y
  442.         1016     031003          ST   Z Store at Z
  443.         1017     261021          BUPRNT Go to print
  444.         1018     001001  ADD     LD   X Load X
  445.         1019     011002          AD   Y Add Y
  446.         1020     031003          ST   Z Store at Z
  447.         1021     411000 PRNT     PC  OP Print
  448.         1022     261006          BUSTRT Go back to start
  449.         9999     001006          ENSTRT End of code with starting location
  450.         
  451.         Figure III-8a
  452.         -----------------------------------------------------------------
  453.         0999          0 Origin
  454.           OP          0
  455.            X          0
  456.            Y          0
  457.            Z          0
  458.                       0 Dummy included to reserve read space
  459.          ONE          1 Constant one
  460.                  RD  OP Read OP, X, Y
  461.                  LD  OP Load OP
  462.                  SU ONE Find difference for test
  463.                  BN*+4  If < 1 go to halt
  464.                  BZ*+8  If = 1 go to add routine
  465.                  SU ONE If > 1 see if it is 2
  466.                  BZ*+2  If = 2 go to subtract
  467.                  HT0000 If not stop
  468.                  LD   X Load X
  469.                  SU   Y Subtract Y
  470.                  ST   Z Store at Z
  471.                  BU*+4  Go to print
  472.                  LD   X Load X
  473.                  AD   Y Add Y
  474.                  ST   Z Store at Z
  475.                  PC  OP Print
  476.                  BU*-16 Go back to start
  477.                  EN*-17 End of code with starting location
  478.         
  479.         Figure III-9
  480.         -----------------------------------------------------------------
  481.                                       III-8
  482.  
  483.  
  484.         Figure III-9 shows the same program as Figure III-8 but using 
  485.         relative addressing instead of named locations.  When assembled, 
  486.         it will result in the same storage assignments as shown earlier 
  487.         in Figure III-8a.
  488.         
  489.         -----------------------------------------------------------------
  490.                       |
  491.                       v
  492.                   -----------
  493.                  / Read MAX /
  494.                  -----------
  495.                       |
  496.                       v
  497.                   +-------+                      +------+
  498.                   | I = 0 |                      | STOP |
  499.                   +-------+                      +------+
  500.                       |                             ^
  501.                       v                             |
  502.                  ------------                -----------------
  503.                 / Read X(I) /<--+           / Print MAX,S,S2 /
  504.                 ------------    |           -----------------
  505.                       |         |                   ^
  506.                       v         |                   | >=
  507.                 +-----------+   |                  / \   
  508.                 | I = I + 1 |   |                 /   \ <
  509.                 +-----------+   |                <I:MAX>-----------+
  510.                       |         |                 \   /            |
  511.                       v         |                  \./             |
  512.                      / \        |                   ^              |
  513.                     /   \ <     |                   |              |
  514.                    <I:MAX>------+             +-----------+        | 
  515.                     \   /                     | I = I + 1 |        |
  516.                      \./                      +-----------+        |
  517.                       | >=                          ^              |
  518.                       v                             |              |
  519.                   +-------+             +-----------------------+  |
  520.                   | I = 0 |             |  S = S + X(I)         |  |
  521.                   | S = 0 |------------>| S2 = S2 + X(I) * X(I) |<-+
  522.                   |S2 = 0 |             +-----------------------+
  523.                   +-------+
  524.         
  525.                                 Exercise III-1
  526.         -----------------------------------------------------------------
  527.         
  528.         As exercises, re-code Exercises I-1 and II-1 in TRAP.  Code 
  529.         Exercise III-1 in TRAP also.  In Exercise III-1, the program 
  530.         first requests the total number of values that are to be read on 
  531.         subsequent reads.  It then reads that number of values and stores 
  532.         them in an array.  You must be sure to reserve enough memory for 
  533.         that array.  It is suggested that the definition of X be the last 
  534.         one of the coding.  That will allow all the remainder of memory 
  535.         to be available for the array.  You may define it earlier but any 
  536.         subsequent code must be re-origined to non-conflicting locations.  
  537.         After the array has been loaded, it is scanned and the sum of the 
  538.         X's and the sum of the squares of the X's is found and displayed.  
  539.         When this program is run, choose values small enough to not cause 
  540.         any overflows.
  541.                                       III-9
  542.